package com.peking.donations.model;

import com.alibaba.druid.util.StringUtils;
import com.google.common.base.Strings;
import com.peking.donations.base.model.BaseModelAuditable;
import com.peking.donations.util.CommonUtil;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wyz on 16/4/26.
 */

@Entity(name = "DonationUser")
@Table(name = "donation_user")
public class DonationUser extends BaseModelAuditable{

    public final static int TYPE_XY = 0;//北大校友
    public final static int TYPE_BDZY = 1;//北大之友
    public final static int TYPE_DEFAULT = -1;//默认类型


    /**
     * 微信openId
     */
    @Column(name="weixin_open_id")
    private String weixinOpenId;

    /**
     * 微信code
     */
    @Column(name="weixin_union_id")
    private String weixinUnionId;

    /**
     * 昵称
     */
    @Column(name="weixin_nickname")
    private String weixinNickname;

    /**
     * 微信icon
     */
    @Column(name="weixin_img")
    private String weixinHeadImg;

    /**
     * 真实姓名
     */
    @Column(name = "real_name")
    private String realName;

    /**
     * 邮件
     */
    @Column(name = "email")
    private String email;

    /**
     * 手机号
     */
    @Column(name = "phone")
    private String phone;

    /**
     * 年纪
     */
    @Column(name = "grade")
    private String grade;

    /**
     * 院系
     */
    @Column(name = "department")
    private String department;

    /**
     * 院系
     */
    @Column(name = "department_code")
    private String departmentCode;



    /**
     * 联系地址
     */
    @Column(name = "address")
    private  String address;


    /**
     * 校友类型
     * @return
     */
    private int type =-1;

    public String getWeixinOpenId() {
        return weixinOpenId;
    }

    public void setWeixinOpenId(String weixinOpenId) {
        this.weixinOpenId = weixinOpenId;
    }

    public String getWeixinUnionId() {
        return weixinUnionId;
    }

    public void setWeixinUnionId(String weixinUnionId) {
        this.weixinUnionId = weixinUnionId;
    }

    public String getWeixinNickname() {
        String nickname = weixinNickname;
        try {
            String[] nicknames = weixinNickname.split("_hex_");
            if (!StringUtils.isEmpty(weixinNickname) && nicknames.length > 1) {
                Pattern pat = Pattern.compile("[0-9a-fA-F]{8}$");
                Matcher mat = pat.matcher(nicknames[1]);
                if (mat.find()) {
                    weixinNickname = CommonUtil.hex2bin(nicknames[1]);
                }
                if (Integer.parseInt(nicknames[0]) != weixinNickname.length()) {
                    weixinNickname = nickname;
                }
            }
        } catch (Exception e) {
            return nickname;
        }
        return weixinNickname;
    }

    public void setWeixinNickname(String weixinNickname) {
        if(!Strings.isNullOrEmpty(weixinNickname)){
            this.weixinNickname = this.filterSpecialCharsInNickname(weixinNickname);
        }
    }

    public String getWeixinHeadImg() {
        return weixinHeadImg;
    }

    public void setWeixinHeadImg(String weixinHeadImg) {
        this.weixinHeadImg = weixinHeadImg;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getDepartmentCode() {
        return departmentCode;
    }

    public void setDepartmentCode(String departmentCode) {
        this.departmentCode = departmentCode;
    }

    private String filterSpecialCharsInNickname(String input) {
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher m = emoji.matcher(input);
        String nickName = m.replaceAll("");
        for (int i = 0; i < nickName.length(); i++) {
            //判断四字节
            if (nickName.codePointAt(i) < 0x0000 || nickName.codePointAt(i) > 0xffff) {
                nickName = nickName.length() + "_hex_" + CommonUtil.bin2hex(nickName);
                break;
            }
        }
        return nickName;
    }


    @Override
    public String toString() {
        return "DonationUser{" +
                "weixinOpenId='" + weixinOpenId + '\'' +
                ", weixinUnionId='" + weixinUnionId + '\'' +
                ", weixinNickname='" + weixinNickname + '\'' +
                ", weixinHeadImg='" + weixinHeadImg + '\'' +
                ", realName='" + realName + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", grade='" + grade + '\'' +
                ", department='" + department + '\'' +
                ", address='" + address + '\'' +
                ", type='" + type + '\'' +
                ", departmentCode='" + departmentCode + '\'' +
                '}';
    }
}
